﻿using KeyboardArg;
using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;

namespace CalendarBoard
{
    /// <summary>
    /// CalendarControl.xaml 的交互逻辑
    /// </summary>
    public partial class CalendarControl : UserControl
    {
        private bool _bLoadFinished = false;

        public KeyBoardArgs KeyBoardArgs
        {
            get { return _keyBoardArgs; }
            set { _keyBoardArgs = value; }
        }

        private KeyBoardArgs _keyBoardArgs;

        private const int MSG_NOTIFY_EXIT = 0x400 + 1;

        /// <summary>
        /// 日期选择更新事件
        /// </summary>
        public event SelectedDateChangedEventHandler SelectedDateChanged;
        protected void OnSelectedDateChanged(DateTime dt)
        {
            if (SelectedDateChanged != null)
            {
                SelectedDateChanged(this, new SelectedDateChangedEventArgs(dt));
            }
        }

        public CalendarControl()
        {
            InitializeComponent();
        }

        public CalendarControl(KeyBoardArgs KeyBoardArgs)
        {
            InitializeComponent();
            SetKeyBoardArgs(KeyBoardArgs);
        }

        public void SetKeyBoardArgs(KeyBoardArgs args)
        {
            _keyBoardArgs = args;
            _bLoadFinished = false;
            
            date.DisplayMode = CalendarMode.Decade;
            string dateValue = _keyBoardArgs.Content;
            _bLoadFinished = true;
            if (_keyBoardArgs.DictionaryFileName.Equals("DateBefore"))
            {
                date.DisplayDateEnd = DateTime.Now;
            }
            else if (_keyBoardArgs.DictionaryFileName.Equals("DateLater"))
            {
                date.DisplayDateStart = DateTime.Now;
            }
        }

        private void date_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!_bLoadFinished)
            {
                return;
            }
            DateTime? dp = date.SelectedDate;
            if (dp == null)
            {
                return;
            }
            if (_keyBoardArgs != null)
            {
                OnSelectedDateChanged(dp.Value);
            }
            else
            {
                //this.Close();
            }
        }
    
        private void SetDate(string content)
        {
            if (!string.IsNullOrEmpty(content))
            {
                if (content.Length == 10)
                {
                    try
                    {
                        DateTime dt = DateTime.ParseExact(content, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
                        if (dt != null)
                        {
                            date.SelectedDate = dt;
                        }
                    }
                    catch (Exception)
                    {

                    }
                }
            }
        }

        //private void LoadCustomStyle()
        //{
        //    #region 窗体样式设置
        //    Uri uri = new Uri("/CalendarBoard;Component/CalendarDictionary.xaml", UriKind.Relative);
        //    ResourceDictionary rd = new ResourceDictionary();
        //    rd.Source = uri;
        //    Style style = rd["CalendarStyle"] as Style;
        //    //date.Style= rd["CalendarStyle"] as Style;
        //    date.SetValue(StyleProperty, style);
        //    #endregion
        //}

        private void SetLanguage(string languageCode)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_keyBoardArgs != null)
            {
                SetLanguage(_keyBoardArgs.LanguageCode);
            }

            _bLoadFinished = true;

            //date.SetValue(Button.StyleProperty, Application.Current.Resources["date"]);
            //LoadCustomStyle();
            //MessageBox.Show(date.Width.ToString());
            //MessageBox.Show(date.Height.ToString());
        }
    }
}
